﻿using System;
using System.ComponentModel;
using System.Data.Common;
using System.Drawing;
using System.IO;

namespace YJKDataBase
{
	[DefaultProperty("DbObject")]
	public class DbObject
	{
		[Browsable(false)]
		public DbExData ExData { get; set; }

		[Browsable(false)]
		[Category("属性")]
		[Description("名称")]
		[DisplayName("名称")]
		public string Name
		{
			get
			{
				return this.mName;
			}
			set
			{
				this.mName = value;
			}
		}

		[Category("属性")]
		[Description("版本")]
		[DisplayName("版本")]
		public string Version
		{
			get
			{
				return this.mVersion;
			}
			set
			{
				this.mVersion = value;
			}
		}

		[Category("属性")]
		[Description("日期")]
		[DisplayName("日期")]
		public DateTime Date
		{
			get
			{
				return this.mDdate;
			}
		}

		public void SetDate(DateTime date)
		{
			this.mDdate = date;
		}

		[Category("属性")]
		[Description("描述")]
		[DisplayName("描述")]
		public string Description
		{
			get
			{
				return this.mDescription;
			}
			set
			{
				this.mDescription = value;
			}
		}

		[Browsable(false)]
		public Guid Guid
		{
			get
			{
				return this.mGuid;
			}
		}

		[Browsable(false)]
		public Guid ParentId
		{
			get
			{
				return this.mParentId;
			}
		}

		[Category("属性")]
		[Description("族图片")]
		[DisplayName("图片")]
		public Image Image
		{
			get
			{
				return this.mImage;
			}
			set
			{
				this.mImage = value;
			}
		}

		[Category("属性")]
		[Description("是否能编辑")]
		[DisplayName("受限")]
		public bool Limit
		{
			get
			{
				return this.mLimit;
			}
			set
			{
				this.mLimit = value;
			}
		}

		[Browsable(false)]
		public RelationDatabase Database
		{
			get
			{
				return this.mDatabase;
			}
		}

		public void SetDatabase(RelationDatabase db)
		{
			this.mDatabase = db;
		}

		public DbObject()
		{
			this.mGuid = Guid.NewGuid();
			this.Version = "";
			this.ExData = new DbExData();
		}

		public DbObject(string name, Image img)
		{
			this.mName = name;
			this.mImage = img;
			this.mGuid = Guid.NewGuid();
			this.Version = "";
			this.ExData = new DbExData();
		}

		public DbObject(string name, Image img, Guid guid)
		{
			this.mName = name;
			this.mImage = img;
			this.mGuid = guid;
			this.Version = "";
			this.ExData = new DbExData();
		}

		private string getFamilyName(string fullName)
		{
			int num = fullName.LastIndexOf("\\");
			int num2 = fullName.LastIndexOf(".");
			return fullName.Substring(num + 1, num2 - num - 1);
		}

		public void SetParentId(Guid id)
		{
			this.mParentId = id;
		}

		public bool Remove()
		{
			if (this.Limit)
			{
				throw new Exception("无法删除系统族");
			}
			DbTransaction tr = this.Database.StartTransaction();
			bool result;
			try
			{
				this.Database.ObjectTable.Remove(this, tr);
				this.Database.ObjectFileTable.Remove(this, tr);
				this.Database.CommitTransaction(tr);
				result = true;
			}
			catch
			{
				this.Database.RollbackTransaction(tr);
				result = false;
			}
			return result;
		}

		public bool Rename(string newName)
		{
			if (this.Limit)
			{
				throw new Exception("系统族无法改名");
			}
			this.mName = newName;
			this.mDatabase.ObjectTable.Update(this);
			return true;
		}

		public bool ChangeParent(Parent newParent)
		{
			if (this.Limit)
			{
				throw new Exception("系统族无法移动");
			}
			this.SetParentId(newParent.Guid);
			return this.Database.ObjectTable.Update(this);
		}

		public FileInfo GetObjectFile(string exeName)
		{
			return this.Database.ObjectFileTable[this, exeName];
		}

		public override string ToString()
		{
			return this.Name;
		}

		public override bool Equals(object obj)
		{
			DbObject dbObject = obj as DbObject;
			return dbObject != null && (this.mGuid.Equals(dbObject.mGuid) && this.Date.Equals(dbObject.Date));
		}

		public override int GetHashCode()
		{
			return this.mName.GetHashCode();
		}

		private string mName;

		private string mVersion;

		private DateTime mDdate;

		private string mDescription = "";

		private Guid mGuid;

		private Guid mParentId;

		private Image mImage;

		private bool mLimit;

		protected RelationDatabase mDatabase;
	}
}
